//
//  NSAttributedString+PPKit.swift
//  PPKit
//
//  Created by xiaopin on 2020/12/3.
//  Copyright © 2020 scoresdata.net. All rights reserved.
//

import Foundation
import UIKit

extension NSAttributedString{
    convenience init(title:String,
                     fontSize:CGFloat = 12,
                     weight:UIFont.Weight = .regular,
                     color:UIColor = .black,
                     lineSpac:CGFloat = 0,
                     alignment:NSTextAlignment = .center) {
        let paraStyle = NSMutableParagraphStyle()
        paraStyle.lineSpacing = lineSpac
        paraStyle.alignment = alignment
        let dic = [NSAttributedString.Key.paragraphStyle:paraStyle,
                   NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize, weight: weight),
                   NSAttributedString.Key.foregroundColor: color] as [NSAttributedString.Key:Any]
        self.init(string: title, attributes: dic)
    }
    
    ///富文本属性字符串
    /// fontSizes weights colors 传一个，代表所有的都沿用该属性
    class func richText(titles:[String],
                        fontSizes:[CGFloat] = [16],
                        weights:[UIFont.Weight] = [UIFont.Weight.regular],
                        colors:[UIColor] = [UIColor.black],
                        lineSpac:CGFloat = 0,
                        alignment:NSTextAlignment = .center) -> NSAttributedString? {
        
        if titles.count <= 0 { return nil }
        
        let attString = NSMutableAttributedString()
        
        for (index, item) in titles.enumerated() {
            var fontSize:CGFloat = 16
            var weight = UIFont.Weight.regular
            var color = UIColor.black
            if fontSizes.count < titles.count && fontSizes.count > 0 {
                fontSize = fontSizes[0]
            }else if fontSizes.count >= titles.count && fontSizes.count > 0 {
                fontSize = fontSizes[index]
            }
            if weights.count < titles.count {
                weight = weights[0]
            }else if weights.count >= titles.count && weights.count > 0 {
                weight = weights[index]
            }
            if colors.count < titles.count && colors.count > 0 {
                color = colors[0]
            }else if colors.count >= titles.count && colors.count > 0 {
                color = colors[index]
            }
            let new_att = NSAttributedString(string: item, attributes: [.font:UIFont.systemFont(ofSize:fontSize,weight:weight),
                                                          .foregroundColor:color])
            attString.append(new_att)

        }
        let paraStyle = NSMutableParagraphStyle()
        paraStyle.lineSpacing = lineSpac
        paraStyle.alignment = alignment
        let dic = [NSAttributedString.Key.paragraphStyle:paraStyle] as [NSAttributedString.Key:Any]
        attString.addAttributes(dic, range: NSMakeRange(0, attString.length))
        
        return attString
    }
}

extension NSMutableParagraphStyle{
    /*
    open var lineSpacing: CGFloat
    open var paragraphSpacing: CGFloat
    open var alignment: NSTextAlignment
    open var firstLineHeadIndent: CGFloat
    open var headIndent: CGFloat
    open var tailIndent: CGFloat
    open var lineBreakMode: NSLineBreakMode
    open var minimumLineHeight: CGFloat
    open var maximumLineHeight: CGFloat
    open var baseWritingDirection: NSWritingDirection
    open var lineHeightMultiple: CGFloat
    open var paragraphSpacingBefore: CGFloat
    open var hyphenationFactor: Float
 */
    /// 初始化段落样式
    /// - Parameters:
    ///   - lineSpacing: 行间距
    ///   - paragraphSpacing: 段落间距
    ///   - alignment: 文字对齐方式
    ///   - firstLineHeadIndent: 文字首行缩进
    ///   - headIndent: 头缩进
    ///   - tailIndent: 尾缩进
    ///   - minimumLineHeight: 最小行高
    ///   - maximumLineHeight: 最大行高
    ///   - paragraphSpacingBefore: 段首行空白空间
    convenience init(lineSpacing:CGFloat? = nil,paragraphSpacing:CGFloat? = nil,alignment:NSTextAlignment? = nil,firstLineHeadIndent:CGFloat? = nil,headIndent:CGFloat? = nil, tailIndent:CGFloat? = nil,minimumLineHeight:CGFloat? = nil,maximumLineHeight:CGFloat? = nil,paragraphSpacingBefore:CGFloat? = nil) {
        self.init()
        if let ls = lineSpacing {
            self.lineSpacing = ls
        }
        if let ps = paragraphSpacing {
            self.paragraphSpacing = ps
        }
        if let align = alignment {
            self.alignment = align
        }
        if let flhi = firstLineHeadIndent {
            self.firstLineHeadIndent = flhi
        }
        if let hi = headIndent {
            self.headIndent = hi
        }
        if let ti = tailIndent {
            self.tailIndent = ti
        }
        if let minLH = minimumLineHeight {
            self.minimumLineHeight = minLH
        }
        if let maxLH = maximumLineHeight {
            self.maximumLineHeight = maxLH
        }
        if let psb = paragraphSpacingBefore {
            self.paragraphSpacingBefore = psb
        }
    }
}

extension NSTextAttachment{
    
    /// 初始化附件
    /// - Parameters:
    ///   - icon: 图标
    ///   - bounds: <#bounds description#>
    convenience init(icon:String, bounds:CGRect){
        self.init()
        self.image = UIImage(named: icon)
        self.bounds = bounds
    }
    
    func toAttrStr() -> NSMutableAttributedString{
        return NSMutableAttributedString(attachment: self)
    }
}
